const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const is_egl = b.option(bool, "USE_EGL", "use EGL in linux") orelse false;

    const exe = b.addExecutable(.{
        .name = "sokol-zigbuild",
        // .root_source_file = b.path("src/main.c"),
        .target = target,
        .optimize = optimize,
    });

    exe.addCSourceFile(.{ .file = b.path("src/main.c") });
    exe.linkLibC();
    
    switch(target.result.os.tag){
        .linux => {
            exe.linkSystemLibrary("X11");
            exe.linkSystemLibrary("Xcursor");
            exe.linkSystemLibrary("Xi");
            exe.linkSystemLibrary("dl");
            exe.linkSystemLibrary("pthread");
            exe.linkSystemLibrary("m");
            exe.linkSystemLibrary("GL");
            if(is_egl){
                exe.defineCMacro("__USE_EGL_TRUE_", null);
                exe.linkSystemLibrary("EGL");
            }
        },
        .windows => {
            exe.linkSystemLibrary("gdi32");
            exe.linkSystemLibrary("winmm");
            exe.linkSystemLibrary("opengl32");
        },
        else => {
            @panic("Unsupported OS");
        }
    }

    b.installArtifact(exe);

    const run_cmd = b.addRunArtifact(exe);

    run_cmd.step.dependOn(b.getInstallStep());

    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}
